﻿using Mengsk.ProxyDelivery.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mengsk.ProxyDelivery.Print.Domain.DeliveryNumberGenerator
{
    class SFDeliveryNumberGenerator : DeliveryNumberGenerator
    {
        public override bool AcceptDeliveryCompany(string arg)
        {
            return arg.Contains("顺丰");
        }

        public override string GenenrateNext(string deliveryNumberAlgorithm, string current)
        {
            if (string.IsNullOrWhiteSpace(current) || current.Length != 12)
            {
                throw new Exception("顺丰快递单号不为12位:" + current);
            }

            string area = current.Substring(0, 3);
            string number = current.Substring(3, 8);
            string lastNumber = current.Substring(11, 1);
            string newNumber = number;
            long next = long.Parse(number) + 1;

            if (current[10] != '9')
            {
                newNumber = area + next.ToString("D8") + (int.Parse(lastNumber) + 9) % 10;
                return newNumber;
            }

            if ("0124578".Any(c => c == current[9]))
            {
                newNumber = area + next.ToString("D8") + (int.Parse(lastNumber) + 6) % 10;
                return newNumber;
            }

            if ("36".Any(c => c == current[9]))
            {
                newNumber = area + next.ToString("D8") + (int.Parse(lastNumber) + 5) % 10;
                return newNumber;
            }

            if ("02468".Any(c => c == current[8]))
            {
                newNumber = area + next.ToString("D8") + (int.Parse(lastNumber) + 3) % 10;
                return newNumber;
            }

            if ("1357".Any(c => c == current[8]))
            {
                newNumber = area + next.ToString("D8") + (int.Parse(lastNumber) + 2) % 10;
                return newNumber;
            }

            if ("036".Any(c => c == current[7]))
            {
                newNumber = area + next.ToString("D8") + (int.Parse(lastNumber) + 0) % 10;
                return newNumber;
            }

            if ("124578".Any(c => c == current[7]))
            {
                newNumber = area + next.ToString("D8") + (int.Parse(lastNumber) + 9) % 10;
                return newNumber;
            }

            if ("0".Any(c => c == current[6]))
            {
                newNumber = area + next.ToString("D8") + (int.Parse(lastNumber) + 7) % 10;
                return newNumber;
            }

            if ("12345678".Any(c => c == current[6]))
            {
                newNumber = area + next.ToString("D8") + (int.Parse(lastNumber) + 6) % 10;
                return newNumber;
            }

            if ("012345678".Any(c => c == current[5]))
            {
                newNumber = area + next.ToString("D8") + (int.Parse(lastNumber) + 3) % 10;
                return newNumber;
            }

            if ("0124578".Any(c => c == current[4]))
            {
                newNumber = area + next.ToString("D8") + (int.Parse(lastNumber) + 9) % 10;
                return newNumber;
            }

            if ("36".Any(c => c == current[4]))
            {
                newNumber = area + next.ToString("D8") + (int.Parse(lastNumber) + 8) % 10;
                return newNumber;
            }

            if ("02468".Any(c => c == current[3]))
            {
                newNumber = area + next.ToString("D8") + (int.Parse(lastNumber) + 5) % 10;
                return newNumber;
            }

            if ("1357".Any(c => c == current[3]))
            {
                newNumber = area + next.ToString("D8") + (int.Parse(lastNumber) + 4) % 10;
                return newNumber;
            }

            throw new Exception("顺丰快递无法计算下一个快递单号：" + current);
        }

        public override string GenenrateOnline(Order order, string wuliuId)
        {
            throw new NotImplementedException();
        }

        public override void CancelOnline(WuliuNumber wuliuNumber)
        {
            throw new NotImplementedException();
        }

        public override string GenenrateWuliuId(params string[] orderIds)
        {
            throw new NotImplementedException();
        }
    }
}
